package cn.yuysaln.domain.strategy.repository;

import cn.yuysaln.domain.strategy.model.entity.StrategyAwardEntity;
import cn.yuysaln.domain.strategy.model.entity.StrategyEntity;
import cn.yuysaln.domain.strategy.model.entity.StrategyRuleEntity;
import cn.yuysaln.domain.strategy.model.valobj.RuleTreeVO;
import cn.yuysaln.domain.strategy.model.valobj.StrategyAwardRuleModelVO;
import cn.yuysaln.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 负责策略抽奖模块的数据仓储调用
 */
public interface IStrategyRepository {
    /**
     * 通过策略ID查找其名下所有的奖品Entity
     * @param strategyId
     * @return
     */
    List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId);

    /**
     * 存储抽奖表
     * @param strategyIdStr 策略ID的字符串
     * @param searchRange 查找范围
     * @param shuffleStrategyAwardSearchRateTable 查找表
     */
    void storeStrategyAwardSearchRateTable(String strategyIdStr, Integer searchRange, LinkedHashMap<Integer, Integer> shuffleStrategyAwardSearchRateTable);
    /**
     * 通过策略ID 获取查找范围
     * @param strategyId
     * @return
     */
    int getRateRange(Long strategyId);
    /**
     * 通过策略ID+权重范围所构成的key 获取查找范围
     * @param key 策略ID+权重范围所构成的key
     * @return
     */
    int getRateRange(String  key);

    /**
     * 从查找表中查找key对应的值
     * @param key 查找表对应的key
     * @param rateKey 随机得到的数字
     * @return
     */
    Integer getStrategyAwardAssemble(String key, int rateKey);


    StrategyEntity queryStrategyEntityByStrategyId(Long strategyId);

    StrategyRuleEntity queryStrategyRule(Long strategyId, String ruleWeight);

    String queryStrategyRuleValue(Long strategyId, Integer awardId, String ruleModel);

    //Mapper文件已经做了sql动态拼接
    String queryStrategyRuleValue(Long strategyId, String ruleModel);

    StrategyAwardRuleModelVO queryStrategyAwardRuleModelVO(Long strategyId, Integer awardId);

    RuleTreeVO queryRuleTreeVOByTreeId(String treeId);

    /**
     * 缓存奖品库存
     * @param cacheKey redisKey
     * @param awardCount 库存量
     */
    void cacheStrategyAwardCount(String cacheKey, Integer awardCount);

    /**
     * 库存扣减
     * @param cacheKey redisKey
     * @return
     */
    Boolean subtractionAwardStock(String cacheKey);
    Boolean subtractionAwardStock(String cacheKey, Date endDateTime);

    /**
     * 加入 写延迟 队列
     *
     * @param strategyAwardStockKeyVO 对象值对象
     */
    void awardStockConsumeSendQueue(StrategyAwardStockKeyVO strategyAwardStockKeyVO);


    StrategyAwardStockKeyVO takeQueueValue();

    void updateStrategyAwardStock(Long strategyId, Integer awardId);

    StrategyAwardEntity queryStrategyAwardEntity(Long strategyId, Integer awardId);

    Long queryStrategyIdByActivityId(Long activityId);

    int queryTodayUserRaffleCount(String userId, Long strategyId);

    Map<String, Integer> queryAwardRuleLockCount(String[] treeIds);
}
